我试图弄清楚如何通过R与其他javascript元素通过R进行交互,我想这是通过将server.R
自定义的闪亮对象(理想情况下为json格式?)提供给ui.R
,然后将其转换为javascript数组。我正在进行的代码是:
服务器
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")
)
)
返回服务器错误:
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.)
..因为这显然是错误的方法。没有server.R的代码行output$json <- ...
,结果将像下面这样工作,因此其余代码正常。但我也想以某种方式获取json(或任何其他格式),并触发后续的javascript操作以将其作为数组对象读取。感谢您提供任何指示,如果不清楚我的描述,请提前致歉。
因此,该错误通常意味着您需要包装reactive({})
一些东西,在本例中为您的toJSON
命令。这有效,并显示JSON数据。
用户界面
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")
)
)
服务器
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
}
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句