Select 使用 shiny widget 来自 dataframe 的列并使用 textInput() 修改其名称
Select column from dataframe using shiny widget and modify its name using textInput()
在下面的应用程序中,我想 select 来自 selectInput()
的列名称,然后使用 textInput()
对其进行修改。更新将在 actionButton
之后进行点击
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
uiOutput("column"),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
dataTableOutput("process")
)
)
server <- function(input, output) {
raw<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
raw2<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
output$column<-renderUI({
selectInput("col","Pick a column to change its name",
choices = colnames(raw2()))
})
mydf <- reactiveValues(df = raw2(), names = names(raw2()))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)
修复方法如下
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("col","Pick a column to change its name",
choices = colnames(iris2)),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
dataTableOutput("process")
)
)
server <- function(input, output) {
mydf <- reactiveValues(df = iris2, names = names(iris2))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)
在下面的应用程序中,我想 select 来自 selectInput()
的列名称,然后使用 textInput()
对其进行修改。更新将在 actionButton
之后进行点击
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
uiOutput("column"),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
dataTableOutput("process")
)
)
server <- function(input, output) {
raw<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
raw2<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = T)
})
output$column<-renderUI({
selectInput("col","Pick a column to change its name",
choices = colnames(raw2()))
})
mydf <- reactiveValues(df = raw2(), names = names(raw2()))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)
修复方法如下
library(shiny)
library(shinydashboard)
library(DT)
iris2 <- iris # make a copy, don't want mess up internal dataset
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("col","Pick a column to change its name",
choices = colnames(iris2)),
textInput("text", label = "Set column name", placeholder = "Enter text..."),
actionButton("sub","submit")
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
dataTableOutput("process")
)
)
server <- function(input, output) {
mydf <- reactiveValues(df = iris2, names = names(iris2))
observeEvent(input$sub, {
req(input$text)
mydf$names[mydf$names == input$col] <- input$text
names(mydf$df) <- mydf$names
updateSelectInput(inputId = "col", choices = mydf$names)
})
output$process<-renderDataTable({
mydf$df
})
}
shinyApp(ui, server)