我对R&Shiny完全陌生,这是我的第一个项目,在经历了一些很棒的教程之后,我才能够做到这一点。
我正在尝试在textOutput中显示所选行的值。该代码适用于所选内容,但是我无法将所选行的值显示到textOuput上,因为它显示了[Object Object]。
这是我到目前为止得到的:
library(shiny)
library(data.table)
addr <- as.data.table(read.csv("addresses.csv", header = T, stringsAsFactors = F))
names(addr) [1:4]<- c("STREET ADDRESS","CITY NAME","PROVINCE","POSTAL CODE")
ui <- fluidPage(
br(),
fluidRow(
column(12, div(DT::dataTableOutput("addressTable"), style="font-family:verdana", align="left"))
),
fluidRow(
column(4, div(textOutput("selectedAddress"), align="center"))
)
)
server <- function(input, output) {
output$addressTable <- DT::renderDataTable({addr}, server = T, selection = 'single')
output$selectedAddress <- DT::renderDataTable({
selectedrowindex <<-input$addr_rows_selected
selectedrowindex <<-as.numeric(selectedrowindex)
selectedrow <- (addr[selectedrowindex,])
selectedrow
})
}
shinyApp(ui, server)
答案 0 :(得分:1)
一些调整:
data.table
documentation,输入是通过ID传递给dataTableOutput()
的访问;您实际上想在input$addr_rows_selected
上写input$addressTable_rows_selected
的位置以便找到用DT::dataTableOutput("addressTable")
呈现的表。textOutput("selectedAddress")
),则应使用renderText()
而不是DT::renderDataTable()
。paste(...,collapse = ",")
library(shiny)
library(data.table)
addr <- as.data.table(read.csv("addresses.csv", header = T, stringsAsFactors = F))
names(addr) [1:4]<- c("STREET ADDRESS","CITY NAME","PROVINCE","POSTAL CODE")
ui <- fluidPage(
br(),
fluidRow(
column(12, div(DT::dataTableOutput("addressTable"), style="font-family:verdana", align="left"))
),
fluidRow(
column(4, div(textOutput("selectedAddress"), align="center"))
)
)
server <- function(input, output) {
output$addressTable <- DT::renderDataTable({addr}, server = T, selection = 'single')
output$selectedAddress <- renderText({
selectedrowindex <- input$addressTable_rows_selected
selectedrowindex <- as.numeric(selectedrowindex)
selectedrow <- paste(addr[selectedrowindex,],collapse = ", ")
selectedrow
})
}
shinyApp(ui, server)