2014-11-03 2 views
2

나는 R이 다른 javascript 요소들과 반짝이는 방법을 알아 내려고 노력하고 있는데, 이는 server.R이 반짝이는 객체 (이상적으로는 json 형식이?)을 ui.R으로 변경 한 다음 javascript 배열로 변환합니다. 내 작업 진행 코드는 다음과 같습니다json/data를 반짝이는 자바 스크립트 객체에 건네기

server.R

library(shiny) 

shinyServer(
    function(input, output) { 
    output$species_table <- renderTable({ iris[iris$Species == input$species,] }) 
    output$json <- RJSONIO::toJSON(iris[iris$Species == input$species,], byrow=T, colNames=T) # error line 
    } 
) 

서버 오류 반환

require(shiny) 
specs = as.character(unique(iris$Species)) 
names(specs) = specs 

pageWithSidebar(
    headerPanel("minimal json handling example"), 
    sidebarPanel(selectInput("species", "Species", specs)), 
    mainPanel(
    tableOutput("species_table") 
) 
) 

ui.R :

Error in .getReactiveEnvironment()$currentContext() : 
    Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside 
a reactive expression or observer.) 

을 .. 그것이 분명히 틀렸기 때문에. 접근. 서버없이.의 라인 output$json <- ... 결과가 작동하고 looks like this, 그래서 나머지 코드는 괜찮습니다. 하지만 나는 또한 어떻게 든간에 json (또는 다른 형식)을 가져 와서 배열 객체로 읽는 후속 javascript 액션을 트리거하려고합니다. 내 설명이 불분명 한 경우 미리 알려 주셔서 감사합니다.

답변

2

따라서 일반적으로이 오류는 명령과 관련하여 reactive({})을 다른 것으로 둘러 쌀 필요가 있음을 의미합니다. 이렇게하면 JSON 데이터가 표시됩니다.

ui.r 다른 사람의 이익을 위해

require(shiny) 
specs = as.character(unique(iris$Species)) 
names(specs) = specs 

pageWithSidebar(
    headerPanel("minimal json handling example"), 
    sidebarPanel(selectInput("species", "Species", specs)), 
    mainPanel(
    #tableOutput("species_table") 
    textOutput("json") 
) 
) 

server.r

library(shiny) 

shinyServer(
    function(input, output) { 
    output$species_table <- renderTable({ iris[iris$Species == input$species,] }) 
    output$json <-reactive({ RJSONIO::toJSON(iris[iris$Species == input$species,], 
     byrow=T, colNames=T) })# error line 
    } 
) 
+0

감사 요 .. 객체 데이터 '업데이트되고 확인하기 위해 브라우저의 자바 스크립트 콘솔 로그를 엽니 다 . 당신은 어떻게 든 자바 스크립트 객체로 읽는 것을 어쨌든 얻을 수 있다는 생각을 가지고 있습니까? 그렇게해서 반짝이는 스크립팅과 통합되지 않습니까? – geotheory

+0

@geotheory'tags $ script (HTML()) '을 사용하여 자바 스크립트를 추가 할 수 있습니다. http://shiny.rstudio.com/articles/에서 HTML 태그와 동적 UI 기사를 확인하십시오. 그 외에도 구체적인 예를 들어 새로운 질문을해야 할 수도 있습니다. 행운을 빕니다. –

+0

금이 간다! 감사합니다 VM 존. 아래 예. – geotheory

3

이 작업 공식이다. 누구든지 좀 더 우아한 해결책을 제안 할 수 있다면 고맙겠습니다. 이 내일 확인합니다,

server.R

library(shiny) 
iris <- datasets::iris 
names(iris) <- gsub('[/.]','_',tolower(names(iris))) 

shinyServer(
    function(input, output) { 
    output$json <- reactive({ 
     paste('<script>data=', 
      RJSONIO::toJSON(iris[iris$species == input$species,], byrow=T, colNames=T), 
      ';console.log(data[0]);', # print 1 data line to console 
      '</script>') 
    }) 
    } 
) 

ui.R

require(shiny) 
iris <- datasets::iris 
names(iris) <- gsub('[/.]','_',tolower(names(iris))) 
specs <- as.character(unique(iris$species)) 
names(specs) <- specs 

pageWithSidebar(
    headerPanel("minimal json handling example"), 
    sidebarPanel(selectInput("species", "Species", specs)), 
    mainPanel(
    htmlOutput("json") 
) 
) 
관련 문제